package cn.imvc.app.models;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.PushMessage;
import cn.imvc.app.repositories.PushMessageRepository;
import cn.imvc.core.ModelUtil;
import cn.imvc.data.domain.ViewRequest;
@Service
public class PushMessageModelImpl implements PushMessageModel{

	@Autowired
	PushMessageRepository pushMessageRepository;
	@Autowired
	XingeAppModel xingeAppModel;
	
	public Page<PushMessage> find(ViewRequest viewRequest) {
		return pushMessageRepository.findAll(ModelUtil.OrderByIdDesc(viewRequest));
	}

	public PushMessage save(PushMessage pushMessage) {
		
		return pushMessageRepository.saveAndFlush(pushMessage);
	}

	public PushMessage get(int id) {
		return pushMessageRepository.getOne(id);
	}

	public PushMessage update(int id, PushMessage pushMessage) {
		PushMessage loadPushMessage = pushMessageRepository.getOne(id);
		BeanUtils.copyProperties(pushMessage, loadPushMessage, new String[]{"id"});
		return pushMessageRepository.saveAndFlush(loadPushMessage);
	}

	public void delete(int id) {
		pushMessageRepository.delete(id);
	}

	@Override
	public void updatePushMessage(PushMessage pushMessage) {
		xingeAppModel.pushMessage(pushMessage.getTitle(), pushMessage.getContent(), pushMessage.getUrl());
		pushMessage.setState((byte)1);
		pushMessageRepository.save(pushMessage);
	}

}
